﻿using System;
using System.Linq;
using System.Net;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Ayatta.Contract.Domain;
using Web.Base.Extensions;
using Web.Manage.Controllers;
using System.Collections.Generic;
using Ayatta.Contract.Component;
using Web.Manage.Areas.Sys.Models;

namespace Web.Manage.Areas.Sys.Controllers
{
    public class PaymentController : AdminBaseController
    {

        public PaymentController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public void Index()
        {

        }

        public ActionResult Banks(int index = 1, int size = 20, string name = null, bool? status = null)
        {
            ViewBag.Name = name;
            ViewBag.Status = status;
            var model = PaymentService.GetPagedPaymentBank(index, size, name, status).ToPagedList(index, size);
            return PartialView(model);
        }

        [HttpGet]
        public ActionResult Bank(int? param)
        {
            var model = new PaymentBank();
            if (param.HasValue && param.Value > 0)
            {
                model = PaymentService.GetPaymentBank(param.Value);
            }
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult Bank(int? param, PaymentBank model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;

            if (param.HasValue && param.Value > 0)
            {
                var old = PaymentService.GetPaymentBank(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = PaymentService.UpdatePaymentBank(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/sys/payment/banks") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = PaymentService.CreatePaymentBank(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/sys/payment/banks");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        public ActionResult DeleteBank(int param = 0)
        {
            if (param > 0)
            {
                var result = PaymentService.DeletePaymentBank(param);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/sys/payment/banks") : Json(HttpStatusCode.BadRequest, "删除失败");
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        public ActionResult Platforms()
        {
            var model = PaymentService.GetAllPaymentPlatform();
            return PartialView(model);
        }


        public ActionResult Platform(int param = 0)
        {
            var model = new PaymentPlatform();
            if (param > 0)
            {
                model = PaymentService.GetPaymentPlatform(param);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Platform(int? param, PaymentPlatform model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;

            if (param.HasValue && param.Value > 0)
            {
                var old = PaymentService.GetPaymentPlatform(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = PaymentService.UpdatePaymentPlatform(old);
                    if (result)
                    {
                        MemcachedClient.Remove(CacheKey.Payment.PaymentPlatforms);
                    }
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/sys/payment/platforms") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = PaymentService.CreatePaymentPlatform(model);
                if (result)
                {
                    MemcachedClient.Remove(CacheKey.Payment.PaymentPlatforms);
                    return Json(HttpStatusCode.OK, "添加成功", true, "/sys/payment/platforms");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        public ActionResult DeletePlatform(int param = 0)
        {
            if (param > 0)
            {
                var result = PaymentService.DeletePaymentPlatform(param);
                if (result)
                {
                    MemcachedClient.Remove(CacheKey.Payment.PaymentPlatforms);
                }
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/sys/payment/platforms") : Json(HttpStatusCode.BadRequest, "删除失败");
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        public ActionResult Ebanks(int param = 0, string name = null)
        {
            var model = new List<PaymentEBank>();
            if (param > 0)
            {
                model = PaymentService.GetAllPaymentEBank(param, null, name).ToList();
            }
            ViewBag.Name = name;
            ViewBag.PlatformId = param;
            return PartialView(model);
        }

        public ActionResult EBank(int? param, int platformId = 0)
        {
            var model = new PaymentEBank();

            if (param > 0)
            {
                model = PaymentService.GetPaymentEBank(param.Value, true);
            }
            ViewBag.PlatformId = platformId;
            ViewBag.Banks = PaymentService.GetAvailablePaymentBank(platformId);
            return PartialView(model);

        }

        [HttpPost]
        public ActionResult EBank(int? param, int platformId, PaymentEBank model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;

            if (param.HasValue && param.Value > 0)
            {
                var old = PaymentService.GetPaymentEBank(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = PaymentService.UpdatePaymentEBank(old);
                    if (result)
                    {
                        MemcachedClient.Remove(CacheKey.Payment.EBanks);
                    }
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, rel: "ebank") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                model.PlatformId = platformId;
                var result = PaymentService.CreatePaymentEBank(model);
                if (result)
                {
                    MemcachedClient.Remove(CacheKey.Payment.EBanks);
                    return Json(HttpStatusCode.OK, "添加成功", true, rel: "ebank");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        public ActionResult DeleteEBank(int param = 0)
        {
            if (param > 0)
            {
                var result = PaymentService.DeletePaymentEBank(param);
                if (result)
                {
                    MemcachedClient.Remove(CacheKey.Payment.EBanks);
                }
                return result ? Json(HttpStatusCode.OK, "删除成功", false, rel: "ebank") : Json(HttpStatusCode.BadRequest, "删除失败");
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }
    }
}
